package com.wys.mall.product.service.impl;

import com.wys.common.utils.MyStrUtil;
import com.wys.mall.product.entity.AttrEntity;
import com.wys.mall.product.service.AttrService;
import com.wys.mall.product.vo.AttrGroupWithAttrsVo;
import com.wys.mall.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wys.common.utils.PageUtils;
import com.wys.common.utils.Query;

import com.wys.mall.product.dao.AttrGroupDao;
import com.wys.mall.product.entity.AttrGroupEntity;
import com.wys.mall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {
        String key = String.valueOf(params.get("key"));
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        if (MyStrUtil.isNotEmpty(key)) {
            queryWrapper.and((wq) -> {
                wq.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        if (categoryId != 0) {
            queryWrapper.eq("catelog_id", categoryId);
        }
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }

    /**
     * 根据分类id
     * 查询所有的分组
     * 以及属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //1、查出当前分类下的所有属性分组，
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        //2、查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(item -> {
            AttrGroupWithAttrsVo vo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(item, vo);
            //3、
            List<AttrEntity> relationAttr = attrService.getRelationAttr(vo.getAttrGroupId());
            vo.setAttrs(relationAttr);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {

        List<SpuItemAttrGroupVo> list = baseMapper.getAttrGroupWithAttrsBySpuId(spuId, catalogId);
        return list;
    }

}